<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ JSON.stringify(text) == '{}' ? '添加课程类型' : '修改课程类型' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form-model ref="ruleForm" :model="form" :rules="rules">
					<a-form-model-item label="名称" prop="Name"><a-input v-model="form.Name" /></a-form-model-item>
					<!-- <a-form-model-item label="上级" prop="ParentID">
						<a-select :allowClear="true" v-model="form.ParentID"
						 placeholder="全部" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in ClubTypeList" :key="index" :value="item.ID">{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-model-item> -->
					<a-form-model-item label="上级" prop="ParentID">
						<a-tree-select :allowClear="true" show-search
						v-model="form.ParentID"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							:tree-data="ActivityTypeApplyList" tree-default-expand-all :replace-fields="replaceFields"
							placeholder="请选择上级" @change="ParentChange"></a-tree-select>
					</a-form-model-item>
					<!-- <a-form-model-item label="分值" prop="Score"><a-input type="number" v-model="form.Score" /></a-form-model-item> -->
					<a-form-model-item label="" class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ JSON.stringify(text) == '{}' ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-model-item>
				</a-form-model>
			</div>
		</div>
	</div>
</template>

<script>
import { AddActivityType,UpdateActivityType } from '@/api/follow';
export default {
	props: {
		text: Object,
		ClubTypeList:Array
	},
	watch: {
		text: function(text) {
			if (JSON.stringify(text) == '{}') {
			} else {
				this.form = text;
			}
		},
		ClubTypeList:function(ClubTypeList){
			this.ActivityTypeApplyList = ClubTypeList
			this.ActivityTypeApplyList = this.ActivityTypeApplyList.map(item => {
				return {
					...item,
					title: item.Name
				};
			});
			this.ActivityTypeApplyList = this.toTree(this.ActivityTypeApplyList);
		}
	},
	data() {
		return {
			replaceFields: {
				children: 'children',
				title: 'title',
				key: 'ID',
				value: 'ID'
			},
			treeselect: true,
			ActivityTypeApplyList: [],
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			DepartmenDropdowntList: [],
			form: {
				Name: '',
				ParentID:'',
				Score:''
			},
			rules: {
				Name: [
					{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}
				],
			}
		};
	},
	methods: {
		ParentChange(e){
			if(this.text.ID === e){
				this.form.ParentID = ''
				this.$message.error('自身不能作为上级！')
			}
		},
		toTree(data) {
			let result = [];
			let map = {};
			if (!Array.isArray(data)) {
				return [];
			}
			data.forEach(item => {
				map[item.ID] = item;
			});
			data.forEach(item => {
				let parent = map[item.ParentID];
				if (parent) {
					(parent.children || (parent.children = [])).push(item);
				} else {
					result.push(item);
				}
			});
			return result;
		},
		closeFunction(data) {
			this.$emit('closeFun', data);
			this.form = {};
		},
		handleSubmit(e) {
			this.$refs.ruleForm.validate(async valid => {
				if (valid) {
					let data = this.form;
					let res = {}
					if (JSON.stringify(this.text) == '{}') {
						res = await AddActivityType(data);
					} else {
						res = await UpdateActivityType(data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					}else{
						this.$message.error(res.data.msg);
					}
				} else {
					return false;
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		margin: 20px 50px;
		display: flex;
	}
	/deep/.ant-form-item-label {
		width: 100px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}
	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}
.ant-form-item {
	margin-bottom: 10px;
}

/deep/.ant-form {
	margin-top: 50px;
	padding-bottom: 50px;
}
</style>
